"""
商品模型
"""
class CommodityModel:
    def __init__(self, name="", price=0, cid=0):
        self.name = name
        self.price = price
        self.cid = cid

class CommodityView:

    def __init__(self):
        self.controller = CommodityController()

    def display_menu(self):
        print("1键录入商品信息")
        print("2键显示商品信息")
        print("3键删除商品信息")
        print("4键修改商品信息")

    def select_menu(self):
        item = input("请选择菜单")
        if item == "1":
            self.add_commodity_info()
        elif item == "2":
            self.display_commodity_info()
        elif item =="3":
            self.del_commodity_info()
        elif item == "4":
            self.modify_commodity()


    def add_commodity_info(self):
        com = CommodityModel()
        com.name = input("请输入商品名称：")
        com.price = int(input("请输入商品价格："))
        self.controller.add_commodity(com)

    def display_commodity_info(self):
        for item in self.controller.commodity_list:
            print(f"{item.name}的商品编号为{item.cid},价格是{item.price}")

    def del_commodity_info(self):
        cid = input("请输入商品编号：")
        if self.controller.remove_commodify(cid):
            print("删除成功")
        else:
            print("删除失败")

    def modify_commodity(self):
        com = CommodityModel()
        com.cid = int(input("请输入商品编码："))
        com.name = input("请输入商品名称：")
        com.price = int(input("请输入商品价格："))
        if self.controller.update_commodity(com):
            print("修改成功")
        else:
            print("修改失败")



class CommodityController:

    def __init__(self):
        self.__commodity_list = []
        self.start_id = 1001

    @property # 对于本类，操作私有变量时，属性不执行
    def commodity_list(self):
        return self.__commodity_list

    def add_commodity(self,new_com):
        new_com.cid = self.start_id
        self.start_id += 1
        self.__commodity_list.append(new_com)

    def remove_commodify(self,cid):
        for i in range(len(self.__commodity_list)):
            if self.__commodity_list[i].cid == cid:
                del self.__commodity_list[i]
                return True
        return False

    def update_commodity(self, com):
        for item in self.__commodity_list:
            if item.cid == com.cid:
                item.__dict__ = com.__dict__
                return True
        return False



view = CommodityView()
while True:
    view.display_menu()
    view.select_menu()